package com.weirong.smartvault.backservice.util.statistics;

import com.weirong.smartvault.backservice.entity_model.MoneyLogModel;
import com.weirong.smartvault.backservice.entity_model.StockHistoryModel;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.DeliveryDetailLogRepository;
import com.weirong.smartvault.dao.DeliveryLogRepository;
import com.weirong.smartvault.dao.StockHistoryRepository;
import com.weirong.smartvault.entity.DeliveryDetailLog;
import com.weirong.smartvault.entity.DeliveryLog;
import com.weirong.smartvault.entity.StockHistory;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 于2017/12/22由 KFEB4 创建 .
 */
public class StatisticsCalculation {
    public static final String funcionName = "getValue";
    private static StockHistoryRepository stockHistoryRepository = SpringContextUtil.getBean(StockHistoryRepository.class);
    static class stockHistory{
        public static double getValue(String currencyType, Integer year,Integer month) throws ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
            month+=1;
            Date start = DateUtil.getMonthStart(year,month);
            Date end = DateUtil.getMonthEnd(year,month);
            List<StockHistory> stockHistoryList = stockHistoryRepository.findAllByModifyTimeBetweenAndTypeCode(start, end,currencyType);
            return getStockAvgSum(stockHistoryList);
        }

        public static double getValue(String currencyType, Integer year,Integer month,Integer day) throws ParseException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
            month+=1;
            Date start = DateUtil.getDayStart(year,month,day);
            Date end = DateUtil.getDayEnd(year,month,day);
            List<StockHistory> stockHistoryList = stockHistoryRepository.findAllByModifyTimeBetweenAndTypeCode(start, end,currencyType);
            return  getStockAvgSum(stockHistoryList);
        }
        static double getStockAvgSum(List<StockHistory> stockHistoryList) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
            //按面值划分
            Map<Object,List<StockHistoryModel>> l = ListTool.divideByKey(ListTool.cast(stockHistoryList, StockHistoryModel.class), StockHistoryModel::getDenominationValue);
            double sum=0;
            double tsum;
            for(Object k:l.keySet()){
                tsum = 0;
                for (Object tempStockHistory :l.get(k)){
                    tsum = ((StockHistoryModel) tempStockHistory).getSum();
                }
                sum+=tsum/l.get(k).size();
            }
            return sum;
        }
    }
    static class use{
        private static DeliveryLogRepository deliveryLogRepository = SpringContextUtil.getBean(DeliveryLogRepository.class);
        private static DeliveryDetailLogRepository deliveryDetailLogRepository = SpringContextUtil.getBean(DeliveryDetailLogRepository.class);

        public static double getValue(String currencyType, Integer year,Integer month) throws ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
            month+=1;
            Date start = DateUtil.getMonthStart(year,month);
            Date end = DateUtil.getMonthEnd(year,month);
            return getSum(start,end,currencyType);
        }
        public static double getValue(String currencyType, Integer year,Integer month,Integer day) throws ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
            month+=1;
            Date start = DateUtil.getDayStart(year,month,day);
            Date end = DateUtil.getDayEnd(year,month,day);
            return getSum(start,end,currencyType);
        }

        public static double getSum(Date start,Date end,String currencyType) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
            List<DeliveryLog> stockHistoryList = deliveryLogRepository.findByDeliveryTimeBetween(start, end);
            List ids = ListTool.map(stockHistoryList,DeliveryLog::getId);
            //获取所有的使用细节
            List<DeliveryDetailLog> deliveryDetailLogs = deliveryDetailLogRepository.findAllByDeliveryIdInAndTypeCode(ids,currencyType);
            List<MoneyLogModel> logs = ListTool.cast(deliveryDetailLogs, MoneyLogModel.class);
            double sum =0;
            for(MoneyLogModel log:logs){
                sum += log.getSum();
            }
            return sum;
        }

    }
}
